/**
 * 存储历史边界高亮图层
 */
import sgisRegionBase from "../../sfgis.region.base.js";
import sgisSpatialQuery from "../../sfgis.spatial.query.js";
import data from "../../../../../static/map/map.hl.info";
// import sgisMapBase from "@/utils/map/sfgis.map.base.js";

export default {
  defaultLayer:{},
  hisLayerInfo:[],
  lineColor:["rgb(105,152,235)","rgb(234,200,56)","rgb(228, 26, 28)","rgb(152, 78, 163)","rgb(255, 127, 0)","rgb(77, 175, 74)"],
  /**
   * 根据区划添加边界
   * regionInfos 区划信息
   * type 区划类型   type=1  普通区划 3 特色区 2高新区
   * haschild 是否有下级
   */
  addHIsHLBoundarys(regionInfos, type, map) {
    this.clearHisBoundary(map);
    if(regionInfos === undefined || regionInfos === null || regionInfos.length < 1){
      return;
    }
    let _this = this;
    // 当前正在使用的datasource不在添加
    let dataSource = sgisRegionBase.regionCatalog.getCurrDataSource();
    let mapUrlDatas = data.data;
    let j = 0;
    for(var i = 0 ; i < mapUrlDatas.length ; i++){
      
      let mapUrlData = mapUrlDatas[i];
      let dataurl = mapUrlData.data_service;
      let hisDataSource = mapUrlData.data_source;
     
      if(hisDataSource === dataSource){
        _this.defaultLayer={
          layerName:"his-layer-"+hisDataSource,
          sourceName:"source-" + hisDataSource,
          name:mapUrlData.name,
          lineColor:_this.lineColor[0]
        }
        continue;
      }
      _this.getFeaturesByRegionCodes(
        regionInfos,
        type,
        hisDataSource,
        dataurl ,
        function(serviceResult) {
          // 设置一下
          if (serviceResult.type=="processCompleted" && serviceResult.result.features && serviceResult.result.features !== null &&serviceResult.result.features.features.length!=0) {
            let layerName = "his-layer-" + hisDataSource;
            let sourceName = "source-" + hisDataSource;
            // 判断一下图层是否存在，存在则清除，不存在则新建
            let source = {
              "type": "geojson",
              "data": {
                "type": "Feature",
                "geometry": {
                  "type": "Polygon",
                  "coordinates": [[]]
                }
              }
            };
            let layer = {};
            j = j+1;
            let lineColor = _this.lineColor[j];
            layer = {
              "id": layerName,
              "type": "line",
              "source": sourceName,
              "paint": {
                "line-color":lineColor,  //rgb(234,200,56), rgb(228, 26, 28)    rgb(152, 78, 163)  rgb(255, 127, 0)  rgb(77, 175, 74)
                "line-width": 4,
                "line-opacity": 1
              },
            };
            map.addSource(sourceName,source);
            if(map.getLayer("hl-layer-map-line")){
              map.addLayer(layer,"hl-layer-map-line");
            }else {
              map.addLayer(layer);
            }
            map.getSource(sourceName).setData(serviceResult.result.features);
            _this.hisLayerInfo.push({
              layerName:layerName,
              sourceName:sourceName,
              name:mapUrlData.name,
              lineColor:lineColor
            });
             
          }
          _this.hideHisBoundary(map);
        }
      );
    }
    
  },
  clearHisBoundary(map) {
    for (let i = 0; i < this.hisLayerInfo.length; i++) {
      const layerName = this.hisLayerInfo[i].layerName;
      const sourceName = this.hisLayerInfo[i].sourceName;
      if (map.getSource(sourceName)) {
        map.removeLayer(layerName);
        map.removeSource(sourceName);
      }
    }
    this.hisLayerInfo = [];
  },
  hideHisBoundary(map) {
    for (let i = 0; i < this.hisLayerInfo.length; i++) {
      const layerName = this.hisLayerInfo[i].layerName;
      if (map.getLayer(layerName)) {
        map.setLayoutProperty(layerName, 'visibility', 'none');
      }
    }
  },
  showHisBoundary(map) {
    for (let i = 0; i < this.hisLayerInfo.length; i++) {
      const layerName = this.hisLayerInfo[i].layerName;
      if (map.getLayer(layerName)) {
        map.setLayoutProperty(layerName, 'visibility', 'visible');
      }
    }
  },
  getFeaturesByRegionCodes(regionInfos, type, dataSource, dataUrl , callback) {
    let options = {};
    if (type !== "1") {
      options = {};
    } else {
      let regioncodes = sgisRegionBase.regionUtils.getRegionCodesByRegionInfos(
        regionInfos
      );
      if (regioncodes.length < 1) {
        // 如果没有区划代码直接return 掉
        return;
      }
      // 区划等级一致所以区划所在面一致
      let regionlevel = sgisRegionBase.regionUtils.getRegionLevel(
        regioncodes[0]
      );
      let dataSetName = sgisRegionBase.regionUtils.getDataSetNameByRegionLevel(
        regionlevel
      );
      options = {
        queryParameter: {
          name: dataSetName + ":" + dataSource,
          attributeFilter: this.getSqlConditionsByRegionCodes(regioncodes)
        },
        datasetNames: [dataSource + ":" + dataSetName]
      }; // 查询数据集名称 // 查询数据集名称
      this.getFeaturesBySql(options, dataUrl,callback);
    }
  },
  /**
   * sql 查询 features
   * @param {OBject} options 查询条件
   * @param {Function} callback 回调方法
   */
  getFeaturesBySql(options, dataUrl, callback) {
    let params = new SuperMap.GetFeaturesBySQLParameters(options);
    params.fromIndex = 0;
    params.toIndex = 99999;
    new mapboxgl.supermap
      .FeatureService(dataUrl)
      .getFeaturesBySQL(params, function(serviceResult) {
        if (typeof callback === "function") {
          callback(serviceResult);
        }
      });
  },
  getSqlConditionsByRegionCodes(regioncodes) {
    let sql = "";
    if (regioncodes != null) {
      for (let i = 0, size = regioncodes.length; i < size; i++) {
        if (regioncodes[i].indexOf("#") !== -1) {
          if (i === size - 1) {
            sql +=
              "QH_CODE like '" +
              sgisRegionBase.regionUtils.getPrefixCode(regioncodes[i]) +
              "%' ";
          } else {
            sql +=
              "QH_CODE like '" +
              sgisRegionBase.regionUtils.getPrefixCode(regioncodes[i]) +
              "%' or ";
          }
        } else {
          if (i === size - 1) sql += "QH_CODE ='" + regioncodes[i] + "' ";
          else sql += "QH_CODE ='" + regioncodes[i] + "' or ";
        }
      }
    }
    return sql;
  },
};
